﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MsForgeReboot.Web.Areas.ControlPanel.Controllers
{
    using MsForgeReboot.Framework.Interfaces;
    using MsForgeReboot.Framework.ViewModels;

    public class MediaGalleryController : Controller
    {
        private readonly IMediaGalleryService mediaGalleryService;

        public MediaGalleryController(IMediaGalleryService mediaGalleryService) {
            this.mediaGalleryService = mediaGalleryService;
        }

        public ActionResult Index(int page = 1, int pageSize = 10) {
            return this.View(mediaGalleryService.GetPagedList(page, pageSize));
        }

        [HttpPost]
        public ActionResult Delete(string fileName) {
            this.mediaGalleryService.Delete(fileName);
            return this.Json("success");
        }

        public ActionResult Create() {
            return this.View("Edit", new ImageViewModel());
        }

        [HttpPost]
        public ActionResult Create(ImageViewModel model) {
            this.mediaGalleryService.Save(model);
            return this.RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult List() {

            var result = from file in this.mediaGalleryService.GetList()
                         select new
                         {
                             thumb = string.Format("{0}/thumbnails/{1}", Properties.Settings.Default.MediaGalleryFolder.Trim('~'), file.Name),
                             image = string.Format("{0}/{1}", Properties.Settings.Default.MediaGalleryFolder.Trim('~'), file.Name)
                         };

            return Json(result, JsonRequestBehavior.AllowGet);
        }

    }
}
